package com.woniuxy.produce;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DelayProduce {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String routerkey,String msg,Integer ttl){
        log.info("生产者发送消息");
        rabbitTemplate.convertAndSend("delayExchange", routerkey, msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(ttl);//设置消息的ttl（存活时间）时间
                return message;
            }
        });
    }
}
